/*
 * 1318_poj.cpp
 *
 *  Created on: 2012-5-28
 *      Author: jawinton
 */


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

#define MAX 101

int len[6];
char str[MAX][7];
char sortStr[MAX][7];
vector<int> strIndex;
vector<int> strLen[6];

int compareLen(const void *a, const void *b) {
	return *((char *)a) - *((char *)b);
}

int strCompare(const void *a, const void *b) {
	return strcmp((char *)a, (char *)b);
}

int main (void) {
	int i;
	for (i=0; ; i++) {
		scanf("%s", str[i]);
		if (strcmp(str[i], "XXXXXX") == 0) break;
		strcpy(sortStr[i], str[i]);
		int len = strlen(sortStr[i]);
		qsort(sortStr[i], len, sizeof(char), compareLen);
		strLen[len-1].push_back(i);
	}
	while (true) {
		char word[7];
		scanf("%s", word);
		if (strcmp(word, "XXXXXX") == 0) break;
		int wordLen = strlen(word);
		vector<int>::iterator it;
		qsort(word, wordLen, sizeof(char), compareLen);
		char result[MAX][7];
		i = 0;
		for (it=strLen[wordLen-1].begin(); it!=strLen[wordLen-1].end(); it++) {
			if (strcmp(word, sortStr[*it]) == 0) {
				strcpy(result[i], str[*it]);
				i++;
			}
		}
		qsort(result, i, sizeof(char)*7, strCompare);
		for (int j=0; j<i; j++)
			printf("%s\n", result[j]);
		if (i==0)
			printf("NOT A VALID WORD\n");
		printf("******\n");
	}
	return 0;
}
